package com.yellowpage.ndc.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.ndc.data.ProdClass;
import com.yellowpage.ndc.service.ProdClassService;

public class ProdClassAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	public String RESULT_QUERY_SEL = "querySel";
	public String RESULT_QUERY_SEL_TWO = "querySelTwo";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<ProdClass> prodClassList = new ArrayList<ProdClass>();
	private List<Map> resultList = new ArrayList<Map>();

	private ProdClass prodClass01;
	private ProdClassService prodClassService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_line;
	private String query_classId;
	private String query_cityCode;
	private String query_cityName;
	private String query_engName;
	private String query_pyName;
	private String query_bxName;
	private String query_note;
	private String query_name;
	private String query_type;
	private String query_status;
	private String query_code;

	private String query_code1;
	private String query_name1;
	private String query_code2;
	private String query_name2;

	private String class_code;

	public void prepare() {
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.prodClass01 = prodClassService.getProdClass(Long
					.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("line", query_line);
		queryMap.put("classId", query_classId);
		queryMap.put("cityCode", query_cityCode);
		queryMap.put("cityName", query_cityName);
		queryMap.put("engName", query_engName);
		queryMap.put("pyName", query_pyName);
		queryMap.put("bxName", query_bxName);
		queryMap.put("note", query_note);
		queryMap.put("name", query_name);
		queryMap.put("type", query_type);
		queryMap.put("status", query_status);
		queryMap.put("code", query_code);

		resultList = prodClassService
				.queryProdClassByPage(queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String querySelByPage() {
		log.error("querySelByPage in....................");
		initPageRender();

		Map queryMap = new HashMap();
		// queryMap.put("line", query_line);
		// queryMap.put("classId", query_classId);
		// queryMap.put("cityCode", query_cityCode);
		// queryMap.put("cityName", query_cityName);
		// queryMap.put("engName", query_engName);
		// queryMap.put("pyName", query_pyName);
		// queryMap.put("bxName", query_bxName);
		// queryMap.put("note", query_note);
		// queryMap.put("type", query_type);
		// queryMap.put("status", query_status);
		queryMap.put("code1", query_code1);
		queryMap.put("name1", query_name1);
		queryMap.put("code2", query_code2);
		queryMap.put("name2", query_name2);

		resultList = prodClassService.queryProdClassSelByPage(queryMap,
				pageRender, "1");

		setPageRenderToRequest();

		return RESULT_QUERY_SEL;
	}

	public String querySelTwoByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("line", query_line);
		queryMap.put("classId", query_classId);
		queryMap.put("cityCode", query_cityCode);
		queryMap.put("cityName", query_cityName);
		queryMap.put("engName", query_engName);
		queryMap.put("pyName", query_pyName);
		queryMap.put("bxName", query_bxName);
		queryMap.put("note", query_note);
		queryMap.put("name", query_name);
		queryMap.put("type", query_type);
		queryMap.put("status", query_status);
		queryMap.put("code", query_code);

		resultList = prodClassService.queryProdClassSelByPage(queryMap,
				pageRender, "2");

		setPageRenderToRequest();

		return RESULT_QUERY_SEL_TWO;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {

		prodClassService.saveProdClass(prodClass01);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		System.out.println(prodClass01.getLine().length());
		if (prodClass01.getLine().length() == 0) {
			this.addActionError("逻辑次序 不能为空 !");
		}
		if (prodClass01.getCityCode().length() == 0) {
			this.addActionError("城市代码 不能为空 !");
		}
		if (prodClass01.getCityName().length() == 0) {
			this.addActionError("城市名称 不能为空 !");
		}
		if (prodClass01.getName().length() == 0) {
			this.addActionError("分类名称 不能为空 !");
		}

		if (prodClass01.getCode().length() == 0) {
			this.addActionError("分类代码 不能为空 !");
		}

		this.setValidatorResult("/ndc/ProdClassAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		prodClass01 = prodClassService.getProdClass(id);

		return RESULT_EDIT;
	}

	public String update() {

		prodClassService.updateProdClass(prodClass01);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (prodClass01.getLine().length() == 0) {
			this.addActionError("逻辑次序 不能为空 !");
		}
		if (prodClass01.getCityCode().length() == 0) {
			this.addActionError("城市代码 不能为空 !");
		}
		if (prodClass01.getCityName().length() == 0) {
			this.addActionError("城市名称 不能为空 !");
		}
		if (prodClass01.getName().length() == 0) {
			this.addActionError("分类名称 不能为空 !");
		}

		if (prodClass01.getCode().length() == 0) {
			this.addActionError("分类代码 不能为空 !");
		}
		this.setValidatorResult("/ndc/ProdClassUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.prodClassService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲删除失败 !");
		}

		return queryByPage();
	}

	
	public void exportExcel() {
		Map queryMap = new HashMap();
		queryMap.put("line", query_line);
		queryMap.put("classId", query_classId);
		queryMap.put("cityCode", query_cityCode);
		queryMap.put("cityName", query_cityName);
		queryMap.put("engName", query_engName);
		queryMap.put("pyName", query_pyName);
		queryMap.put("bxName", query_bxName);
		queryMap.put("note", query_note);
		queryMap.put("name", query_name);
		queryMap.put("type", query_type);
		queryMap.put("status", query_status);
		queryMap.put("code", query_code);
		
		resultList = prodClassService.ProdClassexcel(queryMap);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/x-download");
		OutputStream outp = null;
		response.addHeader("Content-Disposition", "attachment;filename="
				+ "ProdClass.xls");
		try {
			outp = response.getOutputStream();
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet("ProdClass");
			HSSFRow row = sheet.createRow(0);
			sheet.autoSizeColumn((short) 0);
			row.createCell(0).setCellValue("分类ID ");
			row.createCell(1).setCellValue("城市代码 ");
			row.createCell(2).setCellValue("城市名称 ");
			row.createCell(3).setCellValue("分类代码 ");
			row.createCell(4).setCellValue("逻辑次序 ");
			row.createCell(5).setCellValue("分类名称 ");
			row.createCell(6).setCellValue("英文分类名 ");
			row.createCell(7).setCellValue("分类名称首拼 ");
			row.createCell(8).setCellValue("分类名称笔序 ");
			row.createCell(9).setCellValue("类型 ");
			row.createCell(10).setCellValue("备注 ");
			row.createCell(11).setCellValue("状态 ");
			row.createCell(12).setCellValue("品牌数 ");
			row.createCell(13).setCellValue("客户数 ");
			
			for (int i = 0; i < resultList.size(); i++) {

				HSSFRow data_row = sheet.createRow(i + 1);
				Map result = (Map) resultList.get(i);
				data_row.createCell(0).setCellValue(
						result.get("CLASS_ID") != null ? result.get(
								"CLASS_ID").toString() : "");
				data_row.createCell(1).setCellValue(
						result.get("CITY_CODE") != null ? result.get(
								"CITY_CODE").toString() : "");
				data_row.createCell(2).setCellValue(
						result.get("CITY_NAME") != null ? result.get(
								"CITY_NAME").toString() : "");
				data_row.createCell(3).setCellValue(
						result.get("CODE") != null ? result.get(
								"CODE").toString() : "");
				data_row.createCell(4).setCellValue(
						result.get("LINE") != null ? result.get("LINE")
								.toString() : "");
				data_row.createCell(5).setCellValue(
						result.get("NAME") != null ? result.get("NAME")
								.toString() : "");
				data_row.createCell(6).setCellValue(
						result.get("ENG_NAME") != null ? result.get("ENG_NAME")
								.toString() : "");
				data_row.createCell(7).setCellValue(
						result.get("PY_NAME") != null ? result.get(
								"PY_NAME").toString() : "");
				data_row.createCell(8).setCellValue(
						result.get("BX_NAME") != null ? result.get("BX_NAME")
								.toString() : "");
				data_row.createCell(9).setCellValue(
						result.get("TYPE") != null ? result.get("TYPE")
								.toString() : "");
				data_row.createCell(10).setCellValue(
						result.get("NOTE") != null ? result.get(
								"NOTE").toString() : "");
				data_row.createCell(11).setCellValue(
						result.get("STATUS") != null ? result.get(
								"STATUS").toString() : "");
				data_row.createCell(12).setCellValue(
						result.get("COUNTGROUP") != null ? result.get(
								"COUNTGROUP").toString() : "");
				data_row.createCell(13).setCellValue(
						result.get("COUNTCUSTOMER") != null ? result.get(
								"COUNTCUSTOMER").toString() : "");
				
			}
			workbook.write(outp);
			response.flushBuffer();
			outp.flush();
			response.reset();

		} catch (Exception e) {
			log.error(e.getMessage(), e);
			this.addActionError("** 生成文件错误 !");
		} finally {
			if (outp != null) {
				try {
					outp.close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
			}
		}
	}

	
	
	/**
	 * getters and setters
	 * 
	 * @return
	 */

	
	public void setProdClassService(ProdClassService prodClassService) {
		this.prodClassService = prodClassService;
	}

	public ProdClass getProdClass01() {
		return prodClass01;
	}

	public void setProdClass01(ProdClass prodClass01) {
		this.prodClass01 = prodClass01;
	}

	public List<ProdClass> getProdClassList() {
		return prodClassList;
	}

	public void setProdClassList(List<ProdClass> prodClassList) {
		this.prodClassList = prodClassList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_line() {
		return this.query_line;
	}

	public void setQuery_line(String line) {
		this.query_line = line;
	}

	public String getQuery_classId() {
		return this.query_classId;
	}

	public void setQuery_classId(String classId) {
		this.query_classId = classId;
	}

	public String getQuery_cityCode() {
		return this.query_cityCode;
	}

	public void setQuery_cityCode(String cityCode) {
		this.query_cityCode = cityCode;
	}

	public String getQuery_cityName() {
		return this.query_cityName;
	}

	public void setQuery_cityName(String cityName) {
		this.query_cityName = cityName;
	}

	public String getQuery_engName() {
		return this.query_engName;
	}

	public void setQuery_engName(String engName) {
		this.query_engName = engName;
	}

	public String getQuery_pyName() {
		return this.query_pyName;
	}

	public void setQuery_pyName(String pyName) {
		this.query_pyName = pyName;
	}

	public String getQuery_bxName() {
		return this.query_bxName;
	}

	public void setQuery_bxName(String bxName) {
		this.query_bxName = bxName;
	}

	public String getQuery_note() {
		return this.query_note;
	}

	public void setQuery_note(String note) {
		this.query_note = note;
	}

	public String getQuery_name() {
		return this.query_name;
	}

	public void setQuery_name(String name) {
		this.query_name = name;
	}

	public String getQuery_type() {
		return this.query_type;
	}

	public void setQuery_type(String type) {
		this.query_type = type;
	}

	public String getQuery_status() {
		return this.query_status;
	}

	public void setQuery_status(String status) {
		this.query_status = status;
	}

	public String getQuery_code() {
		return this.query_code;
	}

	public void setQuery_code(String code) {
		this.query_code = code;
	}

	public String getQuery_code1() {
		return query_code1;
	}

	public void setQuery_code1(String queryCode1) {
		query_code1 = queryCode1;
	}

	public String getQuery_name1() {
		return query_name1;
	}

	public void setQuery_name1(String queryName1) {
		query_name1 = queryName1;
	}

	public String getQuery_code2() {
		return query_code2;
	}

	public void setQuery_code2(String queryCode2) {
		query_code2 = queryCode2;
	}

	public String getQuery_name2() {
		return query_name2;
	}

	public void setQuery_name2(String queryName2) {
		query_name2 = queryName2;
	}

	public String getClass_code() {
		return class_code;
	}

	public void setClass_code(String class_code) {
		this.class_code = class_code;
	}

}
